package com.mszlu.blog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mszlu.blog.admin.common.Result;
import com.mszlu.blog.admin.entity.Admin;
import com.mszlu.blog.admin.entity.Permission;
import com.mszlu.blog.admin.entity.params.PageParam;
import com.mszlu.blog.admin.entity.response.ResultList;
import com.mszlu.blog.admin.mapper.AdminMapper;
import com.mszlu.blog.admin.service.AdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Admin findAdminByUserName(String username) {

        return adminMapper.selectOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUsername, username));
    }

    @Override
    public List<Permission> findPermissionByAdminId(Long id) {

        return adminMapper.findPermissionByAdminId(id);
    }

    @Override
    public Result userList(PageParam pageParam) {

        Page<Admin> page = new Page<>(pageParam.getCurrentPage(),pageParam.getPageSize());
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(pageParam.getQueryString()),Admin::getUsername,pageParam.getQueryString());

        adminMapper.selectPage(page,lambdaQueryWrapper);

        ResultList<Admin> resultList = new ResultList<>();

        List<Admin> collect = page.getRecords().stream().map((item) -> {
            item.setPassword("******");
            return item;
        }).collect(Collectors.toList());

        resultList.setList(collect);
        resultList.setTotal(page.getTotal());

        return Result.success(resultList);
    }

    @Override
    public Result editPasswordByUserName(Admin admin) {
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        int i = adminMapper.updateByUserName(admin);
        if (i > 0){
            Result.success("success");
        }
        return Result.error("无该用户");
    }

    @Override
    public Result addAdmin(Admin admin) {
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));

        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Admin::getUsername,admin.getUsername());
        Admin admin1 = adminMapper.selectOne(lambdaQueryWrapper);
        if (admin1 != null){
            return Result.error("用户已存在");
        }

        int i = adminMapper.insert(admin);
        return Result.success("添加成功");
    }


}
